package com.yanqu.road.dao.impl.union;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.UnionWarDetail;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionWarDetailDaoImpl extends TempDataObjectDao<UnionWarDetail> {

    public Map<Integer, Map<String, UnionWarDetail>> getUnionWarDetailMap(int mark) {
        String sql = "select * from t_u_union_war_detail where mark=?";
        Map<Integer, Map<String, UnionWarDetail>> dataMap = new ConcurrentHashMap<>();
        dataMap.put(mark, new ConcurrentHashMap<>());
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, mark);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarDetail data = parse(rs);
                dataMap.get(mark).put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UnionWarDetail data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_union_war_detail(mark,unionUid,value,lastUpdateTime,checkTime,totalDonateSilver) values(?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getMark());
            call.setString(i++, data.getUnionUid());
            call.setString(i++, JSON.toJSONString(data));
            call.setLong(i++, data.getLastUpdateTime());
            call.setLong(i++, data.getCheckTime());
            call.setString(i++, data.getTotalDonateSilver().toString());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionWarDetail data) {
        PreparedStatement call = null;
        String sql = "update t_u_union_war_detail set value=?,lastUpdateTime=?,checkTime=?,totalDonateSilver=? where mark=? and unionUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, JSON.toJSONString(data));
            call.setLong(i++, data.getLastUpdateTime());
            call.setLong(i++, data.getCheckTime());
            call.setString(i++, data.getTotalDonateSilver().toString());
            call.setInt(i++, data.getMark());
            call.setString(i++, data.getUnionUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionWarDetail parseData(ResultSet rs) throws SQLException {
        String value = rs.getString("value");
        UnionWarDetail data = JSON.parseObject(value, UnionWarDetail.class);
        data.setMark(rs.getInt("mark"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setCheckTime(rs.getLong("checkTime"));
        value = rs.getString("totalDonateSilver");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setTotalDonateSilver(new BigInteger(value));
        }
        return data;
    }
}
